package com.pocket.demo.base.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.pocket.demo.core.mybatisPlus.DefaultDBFieldHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 */
@Configuration
// 【demo-MybatisPlus-1.4】
@MapperScan("com.pocket.demo.**.dao")
public class MybatisPlusConfig {

    /**
     * 配置默认参数填充
     * @return
     */
    @Bean
    public MetaObjectHandler defaultMetaObjectHandler(){
        return new DefaultDBFieldHandler();
    }


    /**
     * 配置插件【demo-MybatisPlus-2.1】
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        return interceptor;
    }

    /**
     * 如果配置多个插件,切记分页最后添加 【demo-MybatisPlus-2.1】
     * @return
     */
    @Bean
    public PaginationInnerInterceptor paginationInnerInterceptor(){
        PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor();
        // 如果有多数据源可以不配具体类型 否则建议配上具体 DbType
        innerInterceptor.setDbType(DbType.MYSQL);
        // 方言实现类
        // innerInterceptor.setDialect(dialect);
        // 单页分页条数限制, 默认无限制
        // innerInterceptor.setMaxLimit(1000L);
        // 溢出总页数后是否进行处理, 默认不处理
        // innerInterceptor.setOverflow(false);
        return innerInterceptor;
    }
}
